package com.puti.doctor.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.puti.doctor.domian.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.doctor.domian.GoodsInfo;
import com.puti.doctor.service.IGoodsInfoService;
import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品-信息 商品-信息Controller
 * 
 * @author puti
 * @date 2021-04-23
 */
@CrossOrigin
@RestController
@RequestMapping("/info")
public class GoodsInfoController extends BaseController
{

    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 查询商品详情列表 分类模糊搜索根据时间倒序
     */
    @GetMapping("/list")
    public TableDataInfo list(GoodsInfo goodsInfo)
    {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品-信息 商品-信息列表
     */
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsInfo goodsInfo) throws IOException
    {
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        ExcelUtil<GoodsInfo> util = new ExcelUtil<GoodsInfo>(GoodsInfo.class);
        util.exportExcel(response, list, "商品-信息 商品-信息数据");
    }

    /**
     * 图片上传 只能传输1MB以下的图片
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file){
        String img = AliyunOSSUtil.uploadMultipartFile(file, "img");
        return img;
    }

    /**
     * 获取商品-信息详细信息
     */
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        return AjaxResult.success(goodsInfoService.selectGoodsInfoById(goodsId));
    }

    /**
     * 新增商品-信息 商品-信息
     */
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsInfo goodsInfo)
    {
        return toAjax(goodsInfoService.insertGoodsInfo(goodsInfo));
    }

    /**
     * 编辑商品-信息上架、下架
     *
     */
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsInfo goodsInfo)
    {
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo));
    }

    /**
     * 删除商品-信息
     */
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsInfoService.deleteGoodsInfoByIds(goodsIds));
    }
}
